package com.atguigu.atcrowdfunding.controller;

import com.atguigu.atcrowdfunding.bean.Permission;
import com.atguigu.atcrowdfunding.service.PermissionService;
import com.atguigu.atcrowdfunding.service.UserService;
import com.atguigu.atcrowdfunding.bean.AJAXResult;
import com.atguigu.atcrowdfunding.bean.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.Period;
import java.util.*;

/**
 * lcd  2020/3/22
 * Description:
 */
@Controller
public class DispatcherController {
    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    @RequestMapping("/main")
    public String main(){
        return "main";
    }

    @RequestMapping("/error")
    public String error(){
        return "error";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
//        session.removeAttribute("loginUser");
        session.invalidate();
        return "redirect:login";
    }

    @RequestMapping("/doLogin")
    public String doLogin(User user, Model model) throws Exception {
        /*String loginacct = user.getLoginacct();
        //tomcat给springmvc是iso
        byte[] bytes = loginacct.getBytes("ISO-8859-1");
        //转成utf-8
        loginacct=new String(bytes,"UTF-8");*/

        User dbUser=userService.query4Login(user);


        if (dbUser == null) {
            model.addAttribute("errorMsg", "登陆帐号或密码不正确,请重新输入");
            return "redirect:login";
        }else{
            return "main";
        }

    }
    @ResponseBody
    @RequestMapping("/doAJAXLogin")
    public Object doAJAXLogin(User user, HttpSession session) throws Exception {
        User dbUser=userService.query4Login(user);
        AJAXResult ajaxResult = new AJAXResult();
        if (dbUser != null) {
            session.setAttribute("loginUser",dbUser);
            /*
             * 查出这个用户下的权限,组装成树,放到session里面
             **/
            List<Permission> permissionList = permissionService.findPermissionsByUser(dbUser);
            Map<Integer,Permission> map=new HashMap<>();
            Set<String> authUriSet = new HashSet<>();
            for (Permission permission : permissionList) {
                map.put(permission.getId(), permission);
                if (StringUtils.isNotBlank(permission.getUrl())) {
                    authUriSet.add(permission.getUrl());
                }
            }
            Permission root=new Permission();
            for (Permission permission : permissionList) {
                if (permission.getPid() == 0) {
                    root=permission;
                }else{
                    Permission parent = map.get(permission.getPid());
                    parent.getChildren().add(permission);
                }
            }
            session.setAttribute("rootPermission",root);
            session.setAttribute("authUriSet",authUriSet);

            ajaxResult.setSuccess(true);
        }else {
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }
}
